package com.goldplusgold.imageloader.lib;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.File;
import java.net.URL;

/**
 * Created by Administrator on 2017/7/10.
 */

public class IShow implements ShowImageInterface {

    private static IShow show;
    Context mContext;
    String mUrl;
    ImageView mImageView;
    BitmapTransformation mBitmapTransformation;
    SimpleTarget mSimpleTarget;
    private IShow(Builder builder){
        this.mContext = builder.mContext;
        this.mUrl = builder.mUrl;
        this.mBitmapTransformation = builder.mBitmapTransformation;
        this.mSimpleTarget = builder.mSimpleTarget;
    }

    public static IShow getInstance(){
        if(show == null){
            synchronized (IShow.class){
                if(show == null){
                    show = new IShow(new Builder());
                }
            }
        }
        return show;
    }

    @Override
    public void show(Fragment fragment, String url, ImageView view) {
        Glide.with(fragment).load(url).into(view);
    }

    public void show(Context context , int mResId , final ImageView view , int width , int height){
        SimpleTarget target2 = new SimpleTarget<Bitmap>(width, height ) {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                view.setBackground(new BitmapDrawable(bitmap));
            }
        };
        Glide.with(context)
                .load(mResId)
                .asBitmap()
                .into(target2);
    }

    @Override
    public void show(Context context, String url, ImageView view) {
        Glide.with(context).load(url).into(view);
    }

    public void showTarget(Context context, String url,SimpleTarget target2) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .into(target2);
    }

    @Override
    public void showRound(final Context context, String url, final ImageView view, final boolean scale) {
        SimpleTarget target2 = new SimpleTarget<Bitmap>(context.getResources().getDimensionPixelSize(R.dimen.lv_item_width), context.getResources().getDimensionPixelSize(R.dimen.lv_item_height) ) {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                view.setBackground(new BitmapDrawable(bitmap));
            }
        };
        Glide.with(context)
                .load(url)
                .asBitmap()
                .transform(new CenterCrop(context) ,new GlideRoundTransform(context,2))
                .into(view);
    }

    public void showRound(final Context context , String url , final ImageView view , int width ,int height){
        SimpleTarget target2 = new SimpleTarget<Bitmap>(width, height) {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                view.setBackground(new BitmapDrawable(bitmap));
            }
        };
        Log.e("tupian" , "height:" + height + ";width:" +width);
        Glide.with(context)
                .load(url)
                .asBitmap()
                .transform(new GlideRoundTransform(context,2))
                .into(target2);

    }

    public void showRound(final Context context , String url , final ImageView view , int errorResId,int width ,int height){
        SimpleTarget target2 = new SimpleTarget<Bitmap>(width, height) {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                view.setBackground(new BitmapDrawable(bitmap));
            }
        };
        Log.e("tupian" , "height:" + height + ";width:" +width);
        Glide.with(context)
                .load(url)
                .asBitmap()
                .transform(new GlideRoundTransform(context,2))
                .error(errorResId)
                .into(target2);

    }



    @Override
    public void showLayoutRound(final Context context, String url, final RelativeLayout view, final boolean scale) {
        SimpleTarget target2 = new SimpleTarget<Bitmap>(context.getResources().getDimensionPixelSize(R.dimen.lv_item_width), context.getResources().getDimensionPixelSize(R.dimen.lv_item_height) ) {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                view.setBackground(new BitmapDrawable(bitmap));

            }
        };
        Glide.with(context)
                .load(url)
                .asBitmap()
                .transform(new CenterCrop(context) , new GlideRoundTransform(context,4))
                .into(target2);
    }



    @Override
    public void showCircle(Context context, String url, ImageView view) {
        Glide.with(context).load(url).transform(new GlideCircleTransform (context)).into(view);
    }

    public void showCircle(Context context , String url , int errorResIDd , ImageView View){
        Glide.with(context).load(url).error(errorResIDd).transform(new GlideCircleTransform(context)).into(View);
    }
    @Override
    public void show(Context context, int id, ImageView view) {

    }

    @Override
    public void show(Context context, File file, ImageView view) {

    }

    @Override
    public void show(Context context, URL url, ImageView view) {
        Glide.with(context).load(url).into(view);
    }

    @Override
    public void show(Context context, String url, int placeId, ImageView view) {

    }


    static class Builder{
        Context mContext;
        String mUrl;
        ImageView mImageView;
        BitmapTransformation mBitmapTransformation;
        SimpleTarget mSimpleTarget;
        public Builder with(Context context){
            mContext = context;
            return this;
        }
        public Builder load(String url){
            mUrl = url;
            return this;
        }

        public Builder into(SimpleTarget target){
            mSimpleTarget = target;
            return this;
        }

        public Builder into(ImageView imageView){
            mImageView = imageView;
            return this;
        }

        public IShow build(){
            return new IShow(this);
        }
    }
}
